<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="资产分类" prop="assetTypeCode">
        <TreeSelect
          v-model="formItem.assetTypeCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择资产分类"
          :disable-branch-nodes="true"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="formItem.name" :maxlength="255" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input v-model="formItem.specification" :maxlength="255" placeholder="请输入规格型号" />
      </el-form-item>
      <!-- <el-form-item label="计量单位" prop="measureUnit">
        <el-input v-model="formItem.measureUnit" placeholder="请输入计量单位" />
      </el-form-item> -->
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="formItem.brand" placeholder="请输入品牌" />
      </el-form-item>
      <el-form-item label="原值" prop="money">
        <el-input-number v-model="formItem.money" :precision="2" :step="1" :max="1000000000" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import TreeSelect from '@neu/biz-core/components/TreeSelect'
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/product'

import { tree as typeTree } from '@neu/dcam-ui/api/assetType'

// 定义一个表单原始项
const formItem = {
  assetTypeCode: null,
  name: '',
  specification: '',
  // measureUnit: '',
  money: '',
  brand: '',
  belongs: ''
}
export default {
  name: 'ProductEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    },
    belongs: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 控件类型
      controlTypeList: [],
      // 文本格式下拉框
      valueTypeList: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '产品名称不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 50) {
              callback(new Error('最多可输入50个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        specification: [{
          required: true,
          message: '规格型号不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 100) {
              callback(new Error('最多可输入100个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        assetTypeCode: [{
          required: true,
          message: '资产分类不能为空',
          trigger: 'change'
        }]
      },
      modal: true,
      /** 资产分类树 */
      typeOptions: []
      // isSelLeaf: false
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    this.tree()
  },
  methods: {
    // 分类树
    tree() {
      typeTree(this.belongs).then(response => {
        // this.typeOptions = response.data
        if (this.belongs === 'sl') {
          typeTree(this.belongs, '3').then(res => {
            this.typeOptions = [...response.data, ...res.data]
          })
        } else {
          this.typeOptions = response.data
        }
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      // if (!this.isSelLeaf) {
      //   this.$message({
      //     showClose: true,
      //     message: '资产分类只能选择叶子节点！',
      //     type: 'warning'
      //   })
      //   return new Promise(resolve => {
      //     resolve(false)
      //   })
      // }

      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.belongs = this.belongs
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
    // // 点击节点名称触发的事件
    // handleNodeClick: function(data) {
    //   if (!data.children || data.children.length === 0) {
    //     this.isSelLeaf = true
    //   } else {
    //     this.$message({
    //       showClose: true,
    //       message: '资产分类只能选择叶子节点！',
    //       type: 'warning'
    //     })
    //     this.isSelLeaf = false
    //   }
    // }
  }
}
</script>
